package controllers

import (
	"github.com/neo4j/neo4j-go-driver/v4/neo4j"
)

var dbUri string = "neo4j://localhost:7687"

func GetNeo4jConn() (neo4j.Driver, neo4j.Session) {
	driver, err := neo4j.NewDriver(dbUri, neo4j.BasicAuth("neo4j", "980115", ""))
	if err != nil {
		panic(err)
	}
	session := driver.NewSession(neo4j.SessionConfig{})
	return driver, session
}

func init() {
}

func (c *MainController) Get() {
	c.TplName = "index.html"
}

func (c *FuncController) Get() { //获取功能模块标签信息
	driver, session := GetNeo4jConn()
	defer driver.Close()
	defer session.Close()
	result, _ := session.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) {
		records, err := tx.Run("match (n:Func) return n.name as name", map[string]interface{}{})
		if err != nil {
			panic(err)
		}
		var functions []string
		for records.Next() {
			r := records.Record()
			rr, _ := r.Get("name")
			functions = append(functions, rr.(string))
		}
		return functions, err
	})
	c.Data["json"] = result
	c.ServeJSON()
}

func (c *SearchController) Get() {
	query := c.GetString("query")
	driver, session := GetNeo4jConn()
	defer driver.Close()
	defer session.Close()

	results, _ := session.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) {
		result, err := tx.Run(`call db.index.fulltext.queryNodes('page',$query) yield node return node.name as product_name,node.case_num as case_num,node.app_class as app_class, node.date as date, 
		node.detail as detail, node.feature as feature, node.idea as idea, node.page_num as page_num, node.order as order,node.product_class as product_class, node.recorder as recorder,
		node.title as title, node.username as username, node.version as version,node.abstract as abstract,id(node) as id order by date desc`, map[string]interface{}{"query": query})
		if err != nil {
			panic(err)
		}
		var pages []PageCase
		for result.Next() {
			var page PageCase
			record := result.Record()
			product_name := record.GetByIndex(0)
			case_num := record.GetByIndex(1)
			app_class := record.GetByIndex(2)
			date := record.GetByIndex(3)
			detail := record.GetByIndex(4)
			feature := record.GetByIndex(5)
			idea := record.GetByIndex(6)
			page_num := record.GetByIndex(7)
			order := record.GetByIndex(8)
			product_class := record.GetByIndex(9)
			recorder := record.GetByIndex(10)
			title := record.GetByIndex(11)
			username := record.GetByIndex(12)
			version := record.GetByIndex(13)
			abstract := record.GetByIndex(14)
			id := record.GetByIndex(15)
			page.Name, _ = product_name.(string)
			page.Case_num, _ = case_num.(string)
			page.App_class, _ = app_class.(string)
			page.Date, _ = date.(string)
			page.Detail, _ = detail.(string)
			page.Feature, _ = feature.(string)
			page.Idea, _ = idea.(string)
			page.Page_num, _ = page_num.(string)
			page.Order, _ = order.(int64)
			page.Product_class, _ = product_class.(string)
			page.Recorder, _ = recorder.(string)
			page.Title, _ = title.(string)
			page.Username, _ = username.(string)
			page.Version, _ = version.(string)
			page.Abstract, _ = abstract.(string)
			idint64, _ := id.(int64)
			//查用户名任务名
			r, e := tx.Run(`match (p:Page)<-[:Include]-(t:Task) where id(p)=$id return t.name as task`, map[string]interface{}{"id": idint64})
			if e != nil {
				panic(e)
			}
			if r.Next() {
				task := r.Record().GetByIndex(0)
				page.Task, _ = task.(string)
			}
			r, e = tx.Run(`match (p:Page)-[c:Contains]->(pic:Pic) where id(p)=$id return c.title as title,c.order as picorder,pic.path as path`,
				map[string]interface{}{"id": idint64})
			if e != nil {
				panic(e)
			}
			for r.Next() {
				records := r.Record()
				var p Pic
				title := records.GetByIndex(0)
				order := records.GetByIndex(1)
				path := records.GetByIndex(2)
				p.Title, _ = title.(string)
				p.Order, _ = order.(int64)
				p.Path, _ = path.(string)
				page.Pics = append(page.Pics, p)
			}
			pages = append(pages, page)

		}
		return pages, nil
	})
	c.Data["json"] = results
	c.ServeJSON()
}

func (c *SearchController) Vers() {
	app := c.GetString("app")
	driver, session := GetNeo4jConn()
	defer driver.Close()
	defer session.Close()
	results, _ := session.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) {
		r, err := tx.Run(`match (a:App{name:$name})-[v:Vernum]->(pic:Pic) return distinct v.num as num`, map[string]interface{}{"name": app})
		if err != nil {
			panic(err)
		}

		var results []Ver
		for r.Next() {
			record := r.Record()
			ver := record.GetByIndex(0)
			verstr, _ := ver.(string)
			results = append(results, Ver{Value: verstr, Vername: verstr})
		}
		return results, nil
	})
	c.Data["json"] = results
	c.ServeJSON()
}
